package com.ay.smartpark.sdk.common.utils;


import com.ay.smartpark.sdk.common.base.DownloadFileBean;
import com.ay.smartpark.sdk.common.base.DownloadFileResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class DownloadFileUtils {

    private static final Logger logger = LoggerFactory.getLogger(DownloadFileUtils.class);

    public static DownloadFileResponse downloadFile(DownloadFileBean downloadFileBean) {
        //判断上传参数
        if (StringUtils.isBlank(downloadFileBean.getTableId())) {
            return DownloadFileResponse.fail("上传文件参数有误");
        }
        if (StringUtils.isBlank(downloadFileBean.getFieldId())) {
            return DownloadFileResponse.fail("上传文件参数有误");
        }
        if (StringUtils.isBlank(downloadFileBean.getRecordId())) {
            return DownloadFileResponse.fail("上传文件参数有误");
        }
        if (StringUtils.isBlank(downloadFileBean.getFileName())) {
            return DownloadFileResponse.fail("上传文件参数有误");
        }
        if (StringUtils.isBlank(downloadFileBean.getPhpSessionId())) {
            return DownloadFileResponse.fail("上传文件参数有误");
        }
        return downloadFileByPlatform(downloadFileBean);
    }

    public static String getDownloadFileUrl(DownloadFileBean downloadFileBean) {
        //判断上传参数
        if (StringUtils.isBlank(downloadFileBean.getTableId())) {
            return "";
        }
        if (StringUtils.isBlank(downloadFileBean.getFieldId())) {
            return "";
        }
        if (StringUtils.isBlank(downloadFileBean.getRecordId())) {
            return "";
        }
        if (StringUtils.isBlank(downloadFileBean.getFileName())) {
            return "";
        }
        if (StringUtils.isBlank(downloadFileBean.getPhpSessionId())) {
            return "";
        }
        return buildDownloadFileUrl(downloadFileBean);
    }

    private static DownloadFileResponse downloadFileByPlatform(DownloadFileBean downloadFileBean) {
        try {
            String imgSrc = buildDownloadFileUrl(downloadFileBean);
            String phpSessionId = downloadFileBean.getPhpSessionId();

            URL url = new URL(imgSrc);
            URLConnection connection = url.openConnection();
            connection.setRequestProperty("Cookie", "PHPSESSID=" + phpSessionId);
            //设置超时间为3秒
            connection.setConnectTimeout(3 * 1000);
            //防止屏蔽程序抓取而返回403错误
            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            InputStream inputStream = connection.getInputStream();
            if (inputStream != null) {
                return DownloadFileResponse.success(inputStream);
            }
            return DownloadFileResponse.fail("inputStream is null");
        } catch (Exception e) {
            logger.error("downloadFileByPlatform error", e);
            return DownloadFileResponse.fail(e.getMessage());
        }
    }

    private static String buildDownloadFileUrl(DownloadFileBean downloadFileBean) {
        StringBuilder builder = new StringBuilder();
        builder.append(downloadFileBean.getWebProtocol());
        builder.append(downloadFileBean.getWebDomain());
        builder.append("/api/datacenter/attach/");
        builder.append(0);
        builder.append(File.separator);
        builder.append(downloadFileBean.getTableId());
        builder.append(File.separator);
        builder.append(downloadFileBean.getFieldId());
        builder.append(File.separator);
        builder.append(downloadFileBean.getRecordId());
        builder.append(File.separator);
        builder.append(downloadFileBean.getFileName());
        builder.append("/1");
        return builder.toString();
    }

}
